﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using Maticsoft.Common;

using System.IO;
using System.Collections;
using System.Xml;

namespace Admin.WorkFlow.FormTable.TaskBook
{
    public partial class TaskBook_Add : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.Params["OrganizeId"] != null && Request.Params["OrganizeId"].ToString() != "")
                {
                    int organizeId = ChinaSoft.Common.Input.SetInt(Request.Params["OrganizeId"]);
                    BindDepartMent(organizeId);
                }
                if (Request.Params[""] != null && Request.Params["ProjectId"].ToString() != "")
                {
                    int projectId = ChinaSoft.Common.Input.SetInt(Request.Params["ProjectId"]);
                    BindProjectInfoByProjectId(projectId);
                    BindEmployee();
                }
                BindXieBanDepartMent();
            }
        }

        /// <summary>
        /// 根据项目编号绑定项目信息
        /// </summary>
        /// <param name="projectId"></param>
        private void BindProjectInfoByProjectId(int projectId)
        {
            ChinaSoft.BLL.BLL_tb_MidSoft_ProjectInfo bllProject = new ChinaSoft.BLL.BLL_tb_MidSoft_ProjectInfo();
            ChinaSoft.Model.Model_tb_MidSoft_ProjectInfo modelProject = new ChinaSoft.Model.Model_tb_MidSoft_ProjectInfo();
            modelProject = bllProject.GetModel(projectId);
            if (modelProject != null)
            {
                lblProjectName.Text = modelProject.ProjectName;
                lblCustomer.Text = modelProject.Customer;
                lblProjectNumber.Text = modelProject.ProjectNumber;
            }
        }
        /// <summary>
        /// 绑定部门信息
        /// </summary>
        /// <param name="organizeId">委托单位ID</param>
        private void BindDepartMent(int organizeId)
        {
            ChinaSoft.BLL.BLL_tb_MidSoft_DepartMent bllDepartMent = new ChinaSoft.BLL.BLL_tb_MidSoft_DepartMent();
            DataSet ds = bllDepartMent.GetList("OrganizeId="+organizeId);
            ChinaSoft.Common.BindControl.BindDropDownList(ddlUnderTakeDepartment,ds,"DepartMentName","DepartMentId");
            ChinaSoft.Common.BindControl.BindDropDownList(ddlMainDepartment, ds, "DepartMentName", "DepartMentId");
        }
        /// <summary>
        /// 绑定员工信息
        /// </summary>
        private void BindEmployee()
        {
            ChinaSoft.BLL.BLL_tb_MidSoft_Users bllUsers = new ChinaSoft.BLL.BLL_tb_MidSoft_Users();
            DataSet ds = bllUsers.GetAllList();
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                #region 主办部门项目负责人
                ChinaSoft.Common.BindControl.BindDropDownList(ddlMainMan, ds, "UserName", "UsersId");
                #endregion

                #region 经办人
                ChinaSoft.Common.BindControl.BindDropDownList(ddlJingBanMan, ds, "UserName", "UsersId");
                #endregion

                #region 生产发展部
                ChinaSoft.Common.BindControl.BindDropDownList(ddlMakeDevelop, ds, "UserName", "UsersId");
                #endregion

                #region 总工程师室
                ChinaSoft.Common.BindControl.BindDropDownList(ddlEngieer, ds, "UserName", "UsersId");
                #endregion

                #region 分管院长
                ChinaSoft.Common.BindControl.BindDropDownList(ddlYuanZhang, ds, "UserName", "UsersId");
                #endregion
            }
        }
        /// <summary>
        /// 保存数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            string strErr = "";
            if (this.txtMainNumber.Text.Trim().Length == 0)
            {
                strErr += "主编号（综合字）不能为空！\\n";
            }
            if (this.txtSubNumber.Text.Trim().Length == 0)
            {
                strErr += "子编号(第N号)不能为空！\\n";
            }
            if (this.ddlUnderTakeDepartment.SelectedItem.Text=="")
            {
                strErr += "承接部门不能为空！\\n";
            }
            if (!PageValidate.IsAllNumber(txtContractMoney.Text))
            {
                strErr += "合同金额格式错误！\\n";
            }
            if (ddlJingBanMan.SelectedItem.Text == "")
            {
                strErr += "经办人未填！\\n";
            }
            if (ddlMakeDevelop.SelectedItem.Text=="")
            {
                strErr += "生产发展部不能为空！\\n";
            }
            if (ddlEngieer.SelectedItem.Text == "")
            {
                strErr += "总工程师室不能为空！\\n";
            }
            if (ddlYuanZhang.SelectedItem.Text=="")
            {
                strErr += "分管院长不能为空！\\n";
            }
            if (strErr != "")
            {
                MessageBox.Show(this, strErr);
                return;
            }
            string MainNumber = this.txtMainNumber.Text;
            string SubNumber = this.txtSubNumber.Text;
            int PmFlowId = ChinaSoft.Common.Input.SetInt(Request.Params["PmFlowId"]);
            int ProjectId = ChinaSoft.Common.Input.SetInt(Request.Params["ProjectId"]);
            DateTime IssueTime = DateTime.Parse(this.txtIssueTime.Value);
            string ProjectType = rbtnA.Checked==true?"0":"1";//0=A ,1=B
            int According =ChinaSoft.Common.Input.SetInt(ddlIssue.SelectedItem.Value);
            int UnderTakeDepartMentId = ChinaSoft.Common.Input.SetInt(ddlUnderTakeDepartment.SelectedItem.Value);
            decimal ContractMoney =ChinaSoft.Common.Input.SetDecimal(txtContractMoney.Text.Trim());
            #region 设计阶段
            string Stage="0";
            if (CheckBox1.Checked)
                Stage = "1";
            else if (CheckBox2.Checked)
                Stage = "2";
            else if (CheckBox3.Checked)
                Stage = "3";
            else if (CheckBox4.Checked)
                Stage = "4";
            else if (CheckBox5.Checked)
                Stage = "5";
            else if (CheckBox6.Checked)
                Stage = "6";
            else if (CheckBox7.Checked)
                Stage = "7";
            else if (CheckBox8.Checked)
                Stage = "8";
            else if (CheckBox9.Checked)
                Stage = "9";
            else if (CheckBox10.Checked)
                Stage = "10";
            #endregion
            int JingBanManId = ChinaSoft.Common.Input.SetInt(ddlJingBanMan.SelectedItem.Value);
            int MakeDevelopId =ChinaSoft.Common.Input.SetInt(ddlMakeDevelop.SelectedItem.Value);
            int EngineerId =ChinaSoft.Common.Input.SetInt(ddlEngieer.SelectedItem.Value);
            int SubDeanId = ChinaSoft.Common.Input.SetInt(ddlYuanZhang.SelectedItem.Value);
            DateTime EndTime = DateTime.Parse(this.txtEndTime.Value);
            string EndTimeDescription = this.txtEndTimeDescription.Text;
            string Description = this.txtDescription.Text;

            ChinaSoft.Model.Model_tb_MidSoft_TaskBook model = new ChinaSoft.Model.Model_tb_MidSoft_TaskBook();
            model.ProjectId = ProjectId;
            model.JingBanId = JingBanManId;
            model.MainNumber = MainNumber;
            model.SubNumber = SubNumber;
            model.PmFlowId = PmFlowId;
            model.IssueTime = IssueTime;
            model.ProjectType = ProjectType;
            model.According = According;
            model.UnderDepartMentId = UnderTakeDepartMentId;
            model.ContractMoney = ContractMoney;
            model.Stage = Stage;
            model.MakeDevelopId = MakeDevelopId;
            model.EngineerId = EngineerId;
            model.SubDeanId = SubDeanId;
            model.EndTime = EndTime;
            model.EndTimeDescription = EndTimeDescription;
            model.Description = Description;
            model.CreateTime = DateTime.Now;
            model.CreateUserId = ChinaSoft.Common.Input.SetInt(Session["currentUserID"]);
            ChinaSoft.BLL.BLL_tb_MidSoft_TaskBook bll = new ChinaSoft.BLL.BLL_tb_MidSoft_TaskBook();
            int taskbookid=bll.Add(model);
            #region 更新
            ChinaSoft.BLL.Common.Bind bllBind = new ChinaSoft.BLL.Common.Bind();
            bllBind.UpdateList("TaskBookId=" + taskbookid + ",OutPut='"+txtMainMoney.Text+"'", "tb_MidSoft_XieBanDepartMent", "CreateUserId=" + ChinaSoft.Common.Input.SetInt(Session["currentUserID"]) + " and TaskBookId=0");
            bllBind.UpdateList("TaskBookId=" + taskbookid, "tb_MidSoft_XieBanDepartMent", "CreateUserId=" + ChinaSoft.Common.Input.SetInt(Session["currentUserID"]) + " and TaskBookId=1");
            bllBind.UpdateList("TaskBookId=" + taskbookid, "tb_MidSoft_SubContractUser", "CreateUserId="+ ChinaSoft.Common.Input.SetInt(Session["currentUserID"]) + " and (TaskBookId=0 or TaskBookId is Null)");
            #endregion
            Response.Write("<script>alert('成功！');top.window.parent.frames.item('workspace').location.reload();window.parent.$('#DivSysiframe').dialog(\"close\");</script>");
        }
        /// <summary>
        /// 添加联系人
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnUnderTakeUserAdd_Click(object sender, EventArgs e)
        {
            ChinaSoft.Model.Model_tb_MidSoft_SubContractUser modelUser = new ChinaSoft.Model.Model_tb_MidSoft_SubContractUser();
            ChinaSoft.BLL.BLL_tb_MidSoft_XieBanDepartMent modelXieBanDepartMent = new ChinaSoft.BLL.BLL_tb_MidSoft_XieBanDepartMent();
        }
        /// <summary>
        /// 添加主办部门负责人
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnMainUserAdd_Click(object sender, EventArgs e)
        {
            if (ddlMainMan.SelectedItem.Text != "")
            {
                ChinaSoft.Model.Model_tb_MidSoft_SubContractUser modelMainUser = new ChinaSoft.Model.Model_tb_MidSoft_SubContractUser();
                ChinaSoft.BLL.BLL_tb_MidSoft_SubContractUser bllMainUser = new ChinaSoft.BLL.BLL_tb_MidSoft_SubContractUser();
                modelMainUser.CreateTime = DateTime.Now;
                modelMainUser.CreateUserId = ChinaSoft.Common.Input.SetInt(Session["currentUserID"]);
                modelMainUser.SubContractUserId = ChinaSoft.Common.Input.SetInt(ddlMainMan.SelectedItem.Value);
                modelMainUser.XieBanId = ChinaSoft.Common.Input.SetInt(HidMainDepartMent.Value);
                modelMainUser.TaskBookId = 0;
                bllMainUser.Add(modelMainUser);
                lblMainMen.Text += ddlMainMan.SelectedItem.Text + ",";
            }
        }
        /// <summary>
        /// 删除最后一个主办部门项目负责人
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnMainUserDel_Click(object sender, EventArgs e)
        {
            ChinaSoft.Model.Model_tb_MidSoft_SubContractUser modelMainUser = new ChinaSoft.Model.Model_tb_MidSoft_SubContractUser();
            ChinaSoft.BLL.BLL_tb_MidSoft_SubContractUser bllMainUser = new ChinaSoft.BLL.BLL_tb_MidSoft_SubContractUser();
            int recordCount=bllMainUser.GetRecordCount("XieBanId=" + HidMainDepartMent.Value + " and CreateUserId=" + ChinaSoft.Common.Input.SetInt(Session["currentUserID"]));
            if (recordCount > 0)
            {
                ChinaSoft.BLL.Common.Bind bllBind = new ChinaSoft.BLL.Common.Bind();
                bllBind.DeleteWhere("tb_MidSoft_SubContractUser", "SubContractId=(select   top   1   SubContractId   from   tb_MidSoft_SubContractUser where XieBanId=0 order by SubContractId Desc)  ");
            }
            string[] strArr = lblMainMen.Text.Split(',');
            string str3 = "";
            if(strArr.Length>0)
            {
                for (int i = 0; i < strArr.Length; i++)
                {
                    if (i >= strArr.Length -2)
                    {
                        str3 += "";
                    }
                    else
                    {
                        str3 += strArr[i] + ",";
                    }
                }
            }
            lblMainMen.Text = str3;
        }

        protected void ddlMainDepartment_SelectedIndexChanged(object sender, EventArgs e)
        {
            ChinaSoft.Model.Model_tb_MidSoft_XieBanDepartMent modelXieBan = new ChinaSoft.Model.Model_tb_MidSoft_XieBanDepartMent();
            ChinaSoft.BLL.BLL_tb_MidSoft_XieBanDepartMent bllXieBan = new ChinaSoft.BLL.BLL_tb_MidSoft_XieBanDepartMent();
            if (ddlMainDepartment.SelectedItem.Text != "")
            {
                if (HidMainDepartMent.Value != "")
                {
                    //修改
                    modelXieBan = bllXieBan.GetModelList("TaskBookId=0 and CreateUserId=" + ChinaSoft.Common.Input.SetInt(Session["currentUserID"]))[0];
                    modelXieBan.DepartMentId = ChinaSoft.Common.Input.SetInt(ddlMainDepartment.SelectedItem.Value);
                    bllXieBan.Update(modelXieBan);
                }
                else
                {
                    //添加
                    modelXieBan.CreateTime = DateTime.Now;
                    modelXieBan.CreateUserId = ChinaSoft.Common.Input.SetInt(Session["currentUserID"]);
                    modelXieBan.DepartMentId = ChinaSoft.Common.Input.SetInt(ddlMainDepartment.SelectedItem.Value);
                    modelXieBan.DepartMentType = 0;
                    modelXieBan.OutPut = 0;
                    modelXieBan.TaskBookId = 0;
                    HidMainDepartMent.Value = bllXieBan.Add(modelXieBan).ToString();
                }
            }
        }


        private void BindXieBanDepartMent()
        {
            ChinaSoft.BLL.Common.Bind bllBind = new ChinaSoft.BLL.Common.Bind();
            DataSet ds=bllBind.GetDataSet("*", "V_XieBanDepartMent", " TaskBookId=0 and DepartMentType=1 and CreateUserId=" + ChinaSoft.Common.Input.SetInt(Session["currentUserID"]));
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                GridView1.DataSource = ds;
                GridView1.DataBind();
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            BindXieBanDepartMent();
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ChinaSoft.BLL.Common.Bind bllBind = new ChinaSoft.BLL.Common.Bind();
                string XieBanId = GridView1.DataKeys[e.Row.RowIndex][0].ToString();
                DataSet ds = bllBind.GetDataSet("*", "V_SubContractUserLook", " XieBanId=" + XieBanId+" and TaskBookId=0 ");
                if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    Label lblMan = e.Row.FindControl("lblProjectMan") as Label;
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        lblMan.Text += ds.Tables[0].Rows[i]["UserName"].ToString()+",";
                    }
                }
            }
        }
        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int ID = (int)GridView1.DataKeys[e.RowIndex].Value;
            ChinaSoft.BLL.Common.Bind bllBind = new ChinaSoft.BLL.Common.Bind();
            bllBind.DeleteWhere("tb_MidSoft_SubContractUser", "TaskBookId=0 and XieBanId="+ID);
            ChinaSoft.BLL.BLL_tb_MidSoft_XieBanDepartMent bll = new ChinaSoft.BLL.BLL_tb_MidSoft_XieBanDepartMent();
            bll.Delete(ID);
            Maticsoft.Common.MessageBox.Show(this, "删除成功！");
            BindXieBanDepartMent();
        }

    }
}